<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.cse210.grocerwe.common.*" %>

<html>
  <body>

		<%
		    UserService userService = UserServiceFactory.getUserService();
		    User user = userService.getCurrentUser();
		    if (user != null) {
		%>
		<p>Hello, <%= user.getNickname() %>! (You can
		<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
		<p>
		<a href="/start">home / check requests </a> &nbsp
		<a href="/db">view item database</a>&nbsp;
		<a href="/groups">view groups</a></p>
		<%
			PersistenceManager pm = PMF.get().getPersistenceManager();
		    Query q = pm.newQuery(ShoppingItem.class);
		    List<ShoppingItem> inventory = (List<ShoppingItem>) q.execute();
		    if (inventory.isEmpty()) {
		%>
		<p>There are no items in the db</p>
				    <table cellpadding="2" cellspacing="2" width="100%">
					<tr>
					<td bgcolor="#cccccc">Item Name</td>
					<td bgcolor="#cccccc">Item Type</td>
					<td bgcolor="#cccccc">Added by</td>
					<td bgcolor="#cccccc">Group</td>
					</tr>
					<tr>
		<%
		    } else {
		%>
		    		<table cellpadding="2" cellspacing="2" width="100%">
					<tr>
					<td bgcolor="#cccccc">Item Name</td>
					<td bgcolor="#cccccc">Item Type</td>
					<td bgcolor="#cccccc">Added by</td>
					<td bgcolor="#cccccc">Group</td>
					</tr>
					<tr>
		<%	
		    for (ShoppingItem item : inventory) {
		%>
					<td><%=item.getName()%></td>
					<td><%=item.getType()%></td>
					<td><%=item.getOwner()%></td>
					<td><%=item.getGroup()%></td>
					<form action="/database" method="post">
						<input type="hidden" name="mode" value="remove">
						<input type="hidden" name="key" value="<%=item.getKey()%>">
						<td><input type="submit" value="delete"></td>
					</form>
					</tr>
		<%	
		        }
		    }
		%>
							
		  <form action="/database" method="post">
					<tr>
					<td><input type="hidden" name="mode" value="add"><input type="text" name="name" size="20" maxlength="20"></td>
					<td><input type="text" name="type" size="20" maxlength="20"></td>
					<td><input type="submit" value="submit" /></td>
					</tr>
		  </form>
		  </table>
		<%
		    } else {
		%>
		<p>Hello! Please
		<a href="<%= userService.createLoginURL(request.getRequestURI()) %>"> Sign in</a> to use site</p>
		<%
		    }
		%>
		
  </body>
</html>